package com.seeyon.apps.enxicun.event;

import com.alibaba.fastjson.JSONObject;
import com.seeyon.apps.enxicun.utils.HttpRequest;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.organization.bo.V3xOrgAccount;
import com.seeyon.ctp.organization.event.AddAccountEvent;
import com.seeyon.ctp.organization.event.DeleteAccountEvent;
import com.seeyon.ctp.organization.event.UpdateAccountEvent;
import com.seeyon.ctp.organization.manager.OrgManager;
import com.seeyon.ctp.util.annotation.ListenEvent;
import com.seeyon.ctp.util.json.JSONUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @Classname AccountEventListener
 * @Description 单位监听
 * @Date 2022/3/24 16:14
 * @Created by Administrator
 */
public class AccountEventListener {
    private static Log log = LogFactory.getLog(MemberEventListener.class);// 打印日
    private OrgManager orgManager = (OrgManager) AppContext.getBean("orgManager");
    private static String ip = AppContext.getSystemProperty("enxicun.ip");
    private static String access_token = AppContext.getSystemProperty("enxicun.access_token");
    private static V3xOrgAccount account = null;
    Map<String, Object> hashMap = null;

    @ListenEvent(
            event = UpdateAccountEvent.class,
            async = true
    )
    public void updateAccount(UpdateAccountEvent event) {
        log.info("修改单位监听");
        account = event.getAccount();
        hashMap = new HashMap<String, Object>();
        hashMap.put("name", account.getName());
        hashMap.put("code", "DW_" + account.getCode());
        hashMap.put("parentCode", null);
        hashMap.put("sortNo", account.getSortId());
        log.info(JSONUtil.toJSONString(hashMap));
        try {
            HttpRequest.getJsonData(ip + "/api/department/add?access_token=" + access_token, JSONUtil.toJSONString(hashMap));
        } catch (Exception e) {
            log.error(e);
        }
    }

    @ListenEvent(
            event = DeleteAccountEvent.class,
            async = true
    )
    public void deleteAccount(DeleteAccountEvent event){
        log.info("删除单位监听");
        account = event.getAccount();
        String code = "DW_"+account.getCode();
        try {
            HttpRequest.getJsonData(ip + "/api/department/delete/"+code+"?access_token=" + access_token, "");
        } catch (Exception e) {
            log.error(e);
        }


    }

    @ListenEvent(
            event = AddAccountEvent.class,
            async = true
    )
    public void addDAccount(AddAccountEvent event) {
        log.info("新增单位监听");
        account = event.getAccount();
        hashMap = new HashMap<String, Object>();
        hashMap.put("name", account.getName());
        hashMap.put("code", "DW_" + account.getCode());
        hashMap.put("parentCode", null);
        hashMap.put("sortNo", account.getSortId());
        log.info(JSONUtil.toJSONString(hashMap));

        try {
            HttpRequest.getJsonData(ip + "/api/department/add?access_token=" + access_token, JSONUtil.toJSONString(hashMap));
        } catch (Exception e) {
            log.error(e);
        }

    }
}
